<template>
  <div>
    <div class="search-container" @click="$router.push('/search')">
      <div class="search">
        <van-icon name="sousuoxiao" class-prefix="icon" size="18" />
        <van-icon name="search" /><span>搜索商品...</span>
      </div>
    </div>
    <div class="cate-container">
      <div class="cates-wrap">
        <div class="cates">
          <div
            v-for="(cate, index) in cates"
            :key="cate.id"
            @click="switchTab(index)"
            :class="[
              'cate',
              {
                active: index === activeIndex,
              },
            ]"
          >
            {{ cate.name }}
          </div>
        </div>
      </div>
      <div class="items-wrap">
        <div class="items">
          <img
            v-if="noshuju"
            class="nv"
            src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170414%2Fd0997aecb8f54e3c822d820ca54a8ff2_th.jpg&refer=http%3A%2F%2Fimg.mp.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1643878195&t=270f47c6f82f18bcd8ba162d0e778ce7"
            alt=""
          />
          <div v-if="noshuju" class="fw">可万一呢？</div>
          <div v-cloak>
            <img
              class="img-"
              v-if="!noshuju"
              src="https://www.dinghuale.com/public/images/banner1_m.jpg"
              alt=""
            />
          </div>
          <div
            class="item"
            @click="clickItem(item.id)"
            v-for="item in items"
            :key="item.name"
          >
            <img :src="item.pic" alt="" class="item-img" />
            <div class="item-info">
              <h5 class="item-name">
                {{ item.name }}
              </h5>
              <p class="item-desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="item-buy">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon
                  name="gouwuche"
                  size="20"
                  color="#ff734c"
                  class-prefix="icon"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <tabbar />
  </div>
</template>

<script>
import Tabbar from '../../components/Tabbar.vue'
import { getCate, goodsItem } from '_api'
export default {
  components: { Tabbar },
  name: 'Cate',
  data () {
    return {
      cates: [],
      items: [],
      activeIndex: 0,
      noshuju: false
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      // 点击进入详情页
      this.$router.push({
        path: '/details',
        query: {
          id
        }
      })
    },
    switchTab (index) {
      // 切换分类
      this.activeIndex = index
      this.recommendItem()
    },
    recommendItem () {
      // 请求对应分类商品
      goodsItem({
        categoryId: this.cates[this.activeIndex].id
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
          this.noshuju = false
        }
        if (res.data.code === 700) {
          this.items = []
          this.noshuju = true
        }
      })
    },
    fetchCates () {
      getCate().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          // 商品初始调用
          this.recommendItem()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

.search-container {
  height: 50px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  .search {
    display: flex;
    align-items: center;
    height: 40px;
    background-color: #eee;
    width: 96%;
    border-radius: 20px;
    color: #666;
    font-size: 0.4rem;
    /deep/ .icon {
      margin: 0 10px;
    }
    .van-icon {
      margin-top: 0.1rem;
      margin-right: 0.5rem;
    }
  }
}
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 50px;
  top: 50px;
  background-color: rgb(174, 175, 79);
  display: flex;
  background-color: #fff;
  .cates-wrap {
    width: 2.5rem;
    height: 100%;
    overflow: auto;
    .cate {
      line-height: 40px;
      font-size: 14px;
      text-align: center;
      color: #333;
      position: relative;
      &.active {
        color: #884e22;
        &:after {
          content: "";
          width: 2px;
          height: 34px;
          position: absolute;
          left: 0;
          top: 3px;
          background-color: #884e22;
        }
      }
    }
  }
  .items-wrap {
    width: 7.5rem;
    height: 100%;
    overflow: auto;
    position: relative;
    [v-cloak]{
    display: none;
}
    .img- {
      width: 100%;
    }
    .fw {
      height: 4.4rem;
      color: rgb(10, 255, 31);
    }
    .nv {
      width: 7.4rem;
    }
    .item {
      display: flex;
      background-color: #fff;
      border-radius: 5px;
      margin-bottom: 10px;
      .item-img {
        width: 3rem;
        height: 3rem;
      }
      .item-info {
        width: 4.5rem;
        padding: 0 10px;
        box-sizing: border-box;
        .item-name {
          font-size: 16px;
          margin-top: 8px;
          color: #333;
        }
        .item-desc {
          width: 4rem;
          font-size: 14px;
          line-height: 18px;
          margin: 10px 0;
          color: #333;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .item-buy {
          display: flex;
          justify-content: space-between;
          .price {
            font-size: 20px;
            color: #884e22;
          }
        }
      }
    }
  }
}
</style>
